home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0014_PERMUTA5.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  1KB  |  53 lines

  1. {
  2. >it. While I'm at it, does anyone have any ideas For an algorithm to generate
  3. >and test all possible combinations of a group of letters For Real Words.
  4.  
  5. I'm sure it wouldn't take long to modify this Program I wrote, which
  6. produces all combinations of "n" numbers.
  7.  
  8. I got the idea from "Algorithms", by Robert Sedgewick.  Recommended.
  9. }
  10. Program ShowPerms;
  11.  
  12. Uses
  13.   Crt;
  14.  
  15. Const
  16.   digits = 4; {How many digits to permute: n digits = n! perms!}
  17.  
  18. Var
  19.   PermArray : Array [1..digits] of Byte; {Permutation holder}
  20.   ThisDigit : Integer;
  21.  
  22. Procedure WritePerm;
  23. Var
  24.   loop : Byte;
  25. begin
  26.   For loop := 1 to 4 do
  27.     Write(PermArray[loop]);
  28.   Writeln;
  29. end;
  30.  
  31. Procedure PermuteAtLevel(Level : Integer);
  32. Var
  33.   loop : Integer;
  34.  
  35. begin
  36.   inc(ThisDigit);
  37.   PermArray[Level] := ThisDigit;
  38.   if ThisDigit = digits then
  39.     Writeperm; {if we've accounted For all digits}
  40.   For loop := 1 to digits do
  41.     if PermArray[loop] = 0 then
  42.       PermuteAtLevel(loop);
  43.   dec(ThisDigit);
  44.   PermArray[Level] := 0;
  45. end;
  46.  
  47. begin
  48.   ClrScr;
  49.   ThisDigit := -1; {Left of Left-hand-side}
  50.   FillChar (PermArray, sizeof(PermArray),#0); {Make it zeroes}
  51.   PermuteAtLevel(0); {Start at the bottom}
  52. end.
  53. -